<template>
  <el-dialog :title="title" width="500px" :visible="show" @close="closeEvent">
    <el-form ref="form" :model="form" label-width="95px" :rules="rules">
      <el-form-item label="角色名称:" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="角色注解:" prop="description">
        <el-input v-model="form.description" />
      </el-form-item>
    </el-form>

    <template #footer>
      <div>
        <el-button @click="closeEvent">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { sysRolePost, sysRoleEdit } from '@/api/setting.js'
export default {
  props: ['show'],
  data() {
    return {
      mode: 'add',
      form: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '必填', trigger: 'change' },
          { min: 1, max: 10, trigger: 'change', message: '请输入1-10位字符' }
        ],
        description: [
          { required: true, message: '必填', trigger: 'change' },
          { min: 1, max: 10, trigger: 'change', message: '请输入1-10位字符' }
        ]
      }
      // isShow:false
    }
  },
  computed: {
    title() {
      return { add: '新增角色', edit: '编辑角色' }[this.mode]
    }
  },
  methods: {
    closeEvent() {
      this.$emit('update:show', false)
    },
    submit() {
      this.$refs.form.validate(async(result) => {
        if (result) {
          if (this.mode === 'add') {
            await sysRolePost(this.form)
            this.$message.success('新增成功')
          } else if (this.mode === 'edit') {
            await sysRoleEdit(this.form)
            this.$message.success('编辑成功')
          }

          this.closeEvent()
          this.$emit('getData')
        }
      })
    }
  }
}
</script>

<style></style>
